package com.example.leo.mycharmingstudent.a0916;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;
import android.widget.Button;

import com.example.leo.mycharmingstudent.R;
import com.example.leo.mycharmingstudent.a0831.RegisterActivity;
import com.example.leo.mycharmingstudent.a0905.ToastUtil;

import java.util.ArrayList;
import java.util.List;

public class RecyclerViewActivity extends AppCompatActivity {

    private RecyclerView rvHero;
    private LinearLayoutManager mLinearLayoutManager;
    private List<HeroEntity> mHeroList = new ArrayList<>();
    //切换LayoutManager
    private Button btnSwitchLayoutManger;
    private MyRecyclerViewAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycler_view);

        btnSwitchLayoutManger = findViewById(R.id.btn_switch_layout_manager_activity_recycler_view);
        rvHero = findViewById(R.id.rv_hero_activity_recycler_view);

        mLinearLayoutManager = new LinearLayoutManager(this);
//        mLinearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
//        mLinearLayoutManager.setReverseLayout(true);
        rvHero.setLayoutManager(mLinearLayoutManager);
        for (int i = 0; i < 10; i++) {
            if (i % 2 == 0) {
                mHeroList.add(new HeroEntity(R.mipmap.hero3, "嫦娥" + i,
                        "神仙，住月宫,神仙，住月宫,神仙，住月宫,神仙，住月宫,神仙，住月宫,神仙，住月宫,神仙，住月宫,神仙，住月宫,神仙，住月宫,神仙，住月宫,神仙，住月宫,神仙，住月宫,神仙，住月宫,神仙，住月宫,神仙，住月宫,神仙，住月宫"));
            }
            mHeroList.add(new HeroEntity(R.mipmap.hero3, "嫦娥" + i, "神仙，住月宫"));
        }

        mAdapter = new MyRecyclerViewAdapter(rvHero, this, mHeroList);
        rvHero.setAdapter(mAdapter);

        mAdapter.setOnItemClickListener(new MyRecyclerViewAdapter.onItemClickListener() {
            @Override
            public void onItemClick(View view, int position, HeroEntity heroEntity) {
                switch (view.getId()) {
                    case R.id.tv_hero_name_item_cardview:
                        ToastUtil.showToast(RecyclerViewActivity.this, heroEntity.getName());
                        break;
                    case R.id.tv_hero_introduce_item_cardview:
                        ToastUtil.showToast(RecyclerViewActivity.this, heroEntity.getIntroduce());
                        break;
                    case R.id.cv_item_cardview:
                        ToastUtil.showToast(RecyclerViewActivity.this, "条目" + position);
                        break;
                }

            }
        });

        btnSwitchLayoutManger.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //切换LayoutManager
                RecyclerView.LayoutManager layoutManager = rvHero.getLayoutManager();
                /*if(layoutManager instanceof LinearLayoutManager){
                    GridLayoutManager gridLayoutManager = new GridLayoutManager(RecyclerViewActivity.this, 2);
                    rvHero.setLayoutManager(gridLayoutManager);
                }else if(layoutManager instanceof GridLayoutManager){
                    //注意，此处有bug
                    *//*
                    GridLayoutManager是LinearLayoutManager的子类
                    layoutManager既是LinearLayoutManager类型，也是GridLayoutManager类型
                    所以导致此处判断不成立
                     *//*
                    StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
                    rvHero.setLayoutManager(staggeredGridLayoutManager);
                }*/
                if (layoutManager.getClass() == LinearLayoutManager.class) {
                    GridLayoutManager gridLayoutManager = new GridLayoutManager(RecyclerViewActivity.this, 2);
                    rvHero.setLayoutManager(gridLayoutManager);
                    ToastUtil.showToast(RecyclerViewActivity.this, "网格");
                } else if (layoutManager.getClass() == GridLayoutManager.class) {
                    StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
                    rvHero.setLayoutManager(staggeredGridLayoutManager);
                    ToastUtil.showToast(RecyclerViewActivity.this, "瀑布流");
                } else {
                    mLinearLayoutManager = new LinearLayoutManager(RecyclerViewActivity.this);
                    rvHero.setLayoutManager(mLinearLayoutManager);
                    ToastUtil.showToast(RecyclerViewActivity.this, "线性");
                }
            }
        });

    }
}
